package com.ncloud.common.exception;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
@RestController
@ControllerAdvice
public class NCloudWebExceptionHandler {
	@ExceptionHandler(value = UserException.class)
    @ResponseBody
    public Object baseErrorHandler(HttpServletRequest req, Exception e) throws Exception {
		UserException userException = (UserException)e;
		JSONObject result = new JSONObject();
		result.put("code", userException.getErrorCode());
		result.put("message", userException.getErrorMessage());
        return result.toJSONString();
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Object defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
		JSONObject result = new JSONObject();
		result.put("code", "8888");
		result.put("message", "系统异常：" + e.getMessage());
		e.printStackTrace();
    	return result.toJSONString();
    }

}
